﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddResults : System.Web.UI.Page
    {
        StudentAL studentAL;
        
        public Guid SelectedStudent
        {
            get
            {
                if (Session["studentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["studentId"].ToString());
                }
            }
            set
            {
                Session["studentId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            //List<MemberDTO> memberDTO = memberAL.GetAllMembers();
           


            /*ddlCommMembrMembrId.DataSource = memberDTO;
            ddlCommMembrMembrId.DataTextField = "FullName";
            ddlCommMembrMembrId.DataValueField = "MemberId";
            ddlCommMembrMembrId.SelectedValue = SelectedMember.ToString();
            ddlCommMembrMembrId.DataBind();*/

            studentAL = new StudentAL();

            List<string> lstOfOLYrs = new List<string>();
            lstOfOLYrs = studentAL.GetStudents().OrderByDescending(b => b.OrdinaryBatchYear).Select(a => a.OrdinaryBatchYear).Distinct().ToList();

            string OLevelYrNotAvailable = "N/A";
            //lstOfOLYrs.Add(OLevelYrNotAvailable);

            //Insert this element at 1sr place
            lstOfOLYrs.Insert(0, OLevelYrNotAvailable);
            ddlALResultOLYr.DataSource = lstOfOLYrs;
            ddlALResultOLYr.DataBind();

            int currentSystemYear = System.DateTime.Now.Year;
            int alresultStartYr = 1950;
            List<int> listOfYrs = new List<int>();
            for (int i = alresultStartYr; i < currentSystemYear ; i++)
            {
                listOfYrs.Add(i);
            }
            ddlALResultsYr.DataSource = listOfYrs;
            ddlALResultsYr.DataBind();


            //base.OnPreRenderComplete(e);

            List<StudentDTO> studentDTO = new List<StudentDTO>();
            if (!ddlALResultOLYr.SelectedValue.Equals("N/A"))
            {
                studentDTO = studentAL.GetStudentByOLYear(ddlALResultOLYr.SelectedValue);
            }
            else
            {
                studentDTO = studentAL.GetStudents();
            }

            ddlALResultStudentName.DataSource = studentDTO.OrderBy(a=>a.FirstName).ToList();
            ddlALResultStudentName.DataTextField = "FullName";
            ddlALResultStudentName.DataValueField = "StudentId";
            ddlALResultStudentName.SelectedValue = SelectedStudent.ToString();
            ddlALResultStudentName.DataBind();



            StudentDTO studentDTOList = studentAL.GetStudentById(new Guid(ddlALResultStudentName.SelectedValue)).SingleOrDefault();
            if (studentDTOList.IndexNo != "")
            {
                txtALResultsSchoolIndexNo.Text = studentDTOList.IndexNo;
            }
            else
            {
                txtALResultsSchoolIndexNo.Text = "";
            }
           
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            /*studentAL = new StudentAL();
            if (!IsPostBack)
            {
                bindResults();
            }*/
        }

        protected void btnALResultSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ALResultsDTO alResultDTO = new ALResultsDTO
                {
                    ALevelResultId = Guid.NewGuid(),
                    StudentId = new Guid(ddlALResultStudentName.SelectedValue),
                    Stream = ddlALResultStream.SelectedValue,
                    Year = Convert.ToInt32( ddlALResultsYr.SelectedValue),
                    NoOfAs = Convert.ToInt32(ddlALResultsNoOfAs.SelectedValue),
                    NoOfBs = Convert.ToInt32(ddlALResultsNoOfBs.SelectedValue),
                    NoOfCs = Convert.ToInt32(ddlALResultsNoOfCs.SelectedValue),
                    NoOfSs = Convert.ToInt32(ddlALResultsNoOfSs.SelectedValue),
                    NoOfFs = Convert.ToInt32(ddlALResultsNoOfFs.SelectedValue),
                    DistrictRank = txtALResultDistRank.Text,
                    IslandRank = txtALResultIslandRank.Text,
                    Notes = txtALResultSpclNote.Text,
                    Aggregate = txtAggregate.Text,
                    //ModifiedBy = "Tharsan",
                    ModifiedTime = DateTime.Now
                };
                int x= Convert.ToInt32(ddlALResultsNoOfAs.SelectedValue)+
                      Convert.ToInt32(ddlALResultsNoOfBs.SelectedValue) + 
                       Convert.ToInt32(ddlALResultsNoOfCs.SelectedValue) + 
                       Convert.ToInt32(ddlALResultsNoOfSs.SelectedValue) + 
                       Convert.ToInt32(ddlALResultsNoOfFs.SelectedValue);
                string result = "";
                StudentAL studentAL = new StudentAL();
                if (x <= 4)
                {
                    result = studentAL.saveALResults(alResultDTO);

                    if (result == "Saved")
                    {
                        lblALResult.Text = "A/L Results Details added Successfully";
                        //txtCommMembrStartDate.Text = "";
                        //txtCommMembrEnDate.Text = "";
                        //txtCommitteeMemberDescription.Text = "";

                    }
                    else if (result == "Updated")
                    {
                        lblALResult.Text = "A/L Results Details updated Successfully";
                        //txtCommMembrStartDate.Text = "";
                        //txtCommMembrEnDate.Text = "";
                        //txtCommitteeMemberDescription.Text = "";
                    }
                    else if (result == "AlreadyExists")
                    {
                        lblALResult.Text = "A/L Results with of student for this year is already exists ";
                    }
                    else
                    {
                        lblALResult.Text = "Your data cannot be added";
                    }
                }
                else
                {
                    lblALResult.Text = "Number Of Subjects exceeded the limit";
                }
               
                
               
            }
            catch (Exception ee)
            {
                lblALResult.Text = "There is an exception occured while inserting the A/L Results details" + ee.ToString();
            }
        }
        private void bindResults()
        {
           
        }

        protected void ddlALResultStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            studentAL = new StudentAL();
            SelectedStudent = new Guid(ddlALResultStudentName.SelectedValue);
            StudentDTO studentDTOList = studentAL.GetStudentById(new Guid(ddlALResultStudentName.SelectedValue)).SingleOrDefault();
            if (studentDTOList.IndexNo != "")
            {
                txtALResultsSchoolIndexNo.Text = studentDTOList.IndexNo;
            }
            else
            {
                txtALResultsSchoolIndexNo.Text = "";
            }
        }

        protected void ddlALResultExamType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlALResultsYr_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlALResultOLYr_SelectedIndexChanged(object sender, EventArgs e)
        {
            //base.OnPreRenderComplete(e);
            bindResults();
        }
    }
}
